package top.shiyiri.connections;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.alibaba.druid.pool.DruidPooledConnection;
import org.junit.Test;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author Aunean
 * @date 2022/1/20 13:06
 * @Description druid数据库连接池的使用
 */
public class DruidTest {

    //方式一
    @Test
    public void test() throws SQLException {
        DruidDataSource source = new DruidDataSource();
        source.setDriverClassName("com.mysql.cj.jdbc.Driver");
        source.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8");
        source.setUsername("root");
        source.setPassword("123456");

        DruidPooledConnection conn = source.getConnection();
        System.out.println(conn);
    }

    //方式二
    @Test
    public void test2() throws Exception {
        Properties pros = new Properties();
        InputStream is = ClassLoader.getSystemResourceAsStream("druid.properties");
        pros.load(is);
        DataSource source = DruidDataSourceFactory.createDataSource(pros);
        Connection conn = source.getConnection();
        System.out.println(conn);
    }
}
